// pages/balance_recharge/balance_recharge.js
var yz_pay = require("../../../../mycomponent/yz_pay/yz_pay");
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
var app = getApp();
Page({
  behaviors: [yz_pay,skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    credit2: 0,
    buttons: [],
    typename: '',
    recharge: '',
    ordersn: '',
    money: '',
    activatDes: {},
    // 选择的充值方式
    pay_type: '',
    balanceLang: '余额',
    priceDifferencePool:false,
    // 充值标签
    tagBalance:false,
    tagName:'',
    plugName:'',
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if(options.name == 'pdp'){
      //#feat#14800 price-difference-pool 插件的充值页 复用样式
      this.setData({
        priceDifferencePool:true
      })
    }
    if(options.name == 'balance_rechargeTag'){
      this.setData({
        tagBalance:true,
      })
    }
    if(options.name == 'YccRecharge'){
      this.setData({
        plugName : "YccRecharge"
      })
    }
    console.log(options.balance);
    if(options.balance){
      this.setData({
        money:options.balance
      });
    }
    this.getData(); // 初始化参数
    try {
      // let value = wx.getStorageSync('balance');
      let value = wx.getStorageSync('balance');
      value = value || "余额";
      if (value) {
        this.setData({
          balanceLang: value
        });
        if(this.priceDifferencePool){
          this.setData({diyname:basic_info.price_difference_pool_name?basic_info.price_difference_pool_name:'资金池'})
        }
      }
    } catch (e) {
      console.log(e);
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  // 初始化参数
  getData() {
    let that = this;
    let urls = "finance.balance.balance";
    if(this.data.priceDifferencePool){
      urls = 'plugin.price-difference-pool.frontend.modules.pool.controllers.recharge.page';
    }else if (this.data.tagBalance){
      urls = "plugin.tag-balance.frontend.recharge.page";
    }else if (this.data.plugName == 'YccRecharge'){
      urls = "plugin.ycc-money.frontend.recharge.index";
    }
    let urlStr = app.getNetAddresss(urls);
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          if(this.data.priceDifferencePool){
            this.setData({
              credit2:res.data.pool,
              buttons:res.data.buttons,
              recharge:1
            })
            return
          }
          if(this.data.tagBalance){
            this.setData({
              credit2:res.data.balance,
              buttons:res.data.button,
              typename:'充值',
              recharge:1,
              diyname:res.data.tag?.tag_name
            })
            return
          }
          if(this.data.plugName == 'YccRecharge'){
            this.setData({
              balanceLang:'余额',
              credit2:res.data.plugin_member.amount,
              recharge:1,
              buttons:res.data.payment_list,
              recharge_double:res.data.recharge_double,
              plugin_name:res.data.plugin_name
            })
            return
          }
          that.setData({
            credit2: res.data.credit2,
            buttons: res.data.buttons,
            typename: res.data.typename,
            recharge: res.data.recharge,
            activatDes: res.data.remark
          });
          for (let i = 0; i < that.data.buttons.length; i++) {
            if (that.data.buttons[i]) {
              that.setData({
                ['buttons[' + i + '].btclass']: that.btnclass(that.data.buttons[i].value)
              });
              console.log(that.data.buttons);
            }
          }
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  moneyinp(e) {
    let val = e.detail.value;
    this.setData({
      money: val
    });
  },
  priceDifferencePool_confirm(type){
    console.log(this.data.money)
    let json ={
      pay_id:type,
      amount:this.data.money
    }
    let urlStr = app.getNetAddresss("plugin.price-difference-pool.frontend.modules.pool.controllers.recharge.apply");
    app._postNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.rechargePay(type, res, res.data.order_sn, "price-difference-pool");
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  // YCC start
  getMergaPay(ids,btn,type){
    let url = app.getNetAddresss("order.merge-pay");
    app._getNetWork({
      url: url,
      data:{
        order_ids:ids
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.data.order_pay_id = res.data.order_pay.id;
          this.data.pid = res.data.order_pay.uid;
          if(btn.other.need_password){
            // ycc资产 充值余额
            this.setData({
              popupSpecs:true
            })
            return
          }
          this.handlePay(btn,type)
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.error(res);
      },
    });
  },
  payKeyboardClose(e){
    this.setData({
      popupSpecs:false
    })
  },
  payKeyboardComplete(e){
    this.getVerifyPassword(e.detail,'','');
  },
  yccConfirm(type,btn){
    let url = app.getNetAddresss("plugin.ycc-money.frontend.create.index");
    app._getNetWork({
      url: url,
      data:{
        ycc_money_price:this.data.money
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let ids = res.data.order_ids;
          this.getMergaPay(ids,btn,type)
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.error(res);
      },
    });
  },
  // 确认充值
  confirm_post(e) {
    let type = e.currentTarget.dataset.val;
    if(app._isTextEmpty(this.data.money)){
      app.tips('请输入有效金额')
      return
    }
    this.setData({
      money: parseFloat(this.data.money),
      pay_type: type
    });
    if(this.data.priceDifferencePool){
      this.priceDifferencePool_confirm(type);
      return
    }
    if (this.data.money <= 0) {
      wx.showToast({
        icon: 'none',
        title: '金额不可低于0',
        duration: 1500
      });
      return;
    }
    if(this.data.plugName == 'YccRecharge'){
      let { btn } = e.currentTarget.dataset;
      this.yccConfirm(type,btn)
      return
    }
    let urlStr = app.getNetAddresss('finance.balance.recharge');
    if(this.data.tagBalance){
      urlStr = app.getNetAddresss('plugin.tag-balance.frontend.recharge.index');
    }
    urlStr += '&client_type=2';
    urlStr += '&app_type=wechat';
    urlStr += '&pay_type=' + this.data.pay_type;
    urlStr += '&recharge_money=' + this.data.money;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        console.log(res.data)
        if (res.result == 1) {
          if(this.data.pay_type == '88'){
            wx.showModal({
                title:'',
                showCancel:false,
                content:'充值成功',
                success:function(res){
                wx.navigateBack({
                    delta:1
                })
                }
            })
            return
          }
          if (app._isTextEmpty(res.data.ordersn) && this.data.pay_type != '140' && this.data.pay_type != 132) {
            wx.showToast({
              icon: 'none',
              title: '参数错误',
              duration: 1500
            });
            return;
          }
          this.setData({
            ordersn: res.data.ordersn
          });
          this.rechargePay(type, res, res.data.ordersn, "balance");
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          if(type == 102){
            if(res.data.eplus_bind_mobile == 1){
              // 绑定手机
              wx.navigateTo({
                url: '/packageF/others/wisdom/wisdomBind/wisdomBind?option=phone&to=pay',
              })
              app.tips(resdata.msg)
            }
          }
        }
      },
      fail:(res)=> {
        console.log(res);
      }
    });
  },
});
